<template>
	<el-form-item>
		<el-select class="w_130" v-model="agent_id" placeholder="请选择代理商" filterable @change="agentChange" :clearable='clearable'>
			<el-option v-for="item in list" :key="item.id" :label="item.name" :value="item.id"></el-option>
		</el-select>
	</el-form-item>
</template>

<script>
import { searchAgentList,searchCityList } from '@/api/agent.js'

export default {
	name: '',
	data() {
		return {
			list: '',
			agent_id: '',
			agent_name: '',
			city_list: '',
			roleType: '',
			clearable: false
		}
	},
	props: {
		reset: {
			type: Number,
			default: 0
		}
	},
	watch: {
		reset(value) {
			this.getList("reset")
		}
	},
	created() {
		this.getList()
		let ret = JSON.parse(sessionStorage.getItem('manageInfo'));
		this.roleType = ret.role_info.type
		if(this.roleType == 1){
			this.clearable = true
		}
	},
	methods: {
		async getList(type) {
			try {
				let ret = await searchAgentList({ status: 1 })
				this.list = ret.list.map(it => {
					return {
						id: it.id,
						name: it.name,
						value: it.name,
						city_list: it.city_list
					}
				})
				let { user_info }= JSON.parse(sessionStorage.getItem('manageInfo'))
				if(user_info.type == 2){ //代理商角色
					this.agent_id = +this.list[0].id
					this.agent_name = this.list[0].name
				}else if(type == "reset"){
					this.agent_id = ""
					this.agent_name = ""
				}
				this.getCityList();
			} catch (err) {
				console.log(err)
			}
		},
		async getCityList() {
			try {
				let ret = await searchCityList({ 
					agent_id: this.agent_id,
					order:{"id":-1}
				})
				this.city_list = ret.list.map(it => {
					return {
						id: it.id,
						name: it.city_name,
						value: it.city_name
					}
				})
				this.listChange()
			} catch (err) {
				console.log(err)
			}
		},
		listChange() {
			this.$emit('sendValue', { agent_id: this.agent_id, city_list: this.city_list })
		},
		agentChange(val) {
			if(val){
				let a = this.list.find(x => x.id == val)
				let res = this.list.filter(x => x.id == val)[0]
				this.agent_name = res.name
				this.agent_id = res.id
				this.city_list = res.city_list
			}
			this.listChange()
		}
	}
}
</script>

<style scoped></style>



// WEBPACK FOOTER //
// src/components/components/Lh-agent-list.vue